package com.truyentranh.modules.news;

import it.sephiroth.android.library.imagezoom.graphics.FastBitmapDrawable;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

import org.xml.sax.SAXException;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.truyentranh.R;
import com.truyentranh.data.RssEntryDataObject;
import com.truyentranh.data.RssEntryDataObject.Entry;
import com.truyentranh.network.APIRequestWorker;
import com.truyentranh.network.Request;
import com.truyentranh.network.RequestListener;
import com.truyentranh.requester.RssRequester;
import com.truyentranh.utils.AppSettingParams;
import com.truyentranh.utils.Logger;

public class RssViewActivity extends Activity {

	private int newId = -1;
	LinearLayout loadingpage;
	LinearLayout tblLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		// Remove notification bar
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//		setContentView(R.layout.testact);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			newId = extras.getInt("NEW_ID");
		}

		loadingpage = (LinearLayout) findViewById(R.id.loadingpage);
//		tblLayout = (LinearLayout) findViewById(R.id.tblLayout);
	}

	boolean isGettingData = false;

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		if (!isGettingData) {
			isGettingData = true;
			initNewsData();
		}
	}

	void doAddNews(LayoutInflater inflater, final ArrayList<RssEntryDataObject> rssDataObjects, final int index) {
		final FrameLayout rowHeader = (FrameLayout) inflater.inflate(R.layout.news_detail_shelf_item, tblLayout, false);
		TextView titleNewsText = (TextView) rowHeader.findViewById(R.id.titleNewsText);
		LinearLayout line = (LinearLayout) rowHeader.findViewById(R.id.tblLayoutLine);

		RssEntryDataObject rssDataObject = rssDataObjects.get(index);
		titleNewsText.setText(rssDataObject.getName());

		ArrayList<Entry> entries = rssDataObject.getAllEntry();

		if (rowHeader.getParent() != null) {
			((ViewGroup) rowHeader.getParent()).removeAllViews();
		}

		for (int k = 0; k < entries.size(); k++) {
			final Entry dataObject = entries.get(k);

			FrameLayout renderDetail = (FrameLayout) inflater.inflate(R.layout.news_render_detail_modules, line, false);
			final TextView nameViewNews = (TextView) renderDetail.findViewById(R.id.nameView);

			runOnUiThread(new Runnable() {

				public void run() {
					boolean memCache = false;
					boolean fileCache = true;
					Logger.logMe("dataObject.getImg() " + dataObject.getImg());
					aQuery.id(R.id.avatarView).image(dataObject.getImg(), memCache, fileCache);
				}
			});

			nameViewNews.setText(dataObject.getTitle());
			line.addView(renderDetail);
		}		
		
		tblLayout.addView(rowHeader, index);
	}

	AQuery aQuery;

	private void initRssData(final ArrayList<RssEntryDataObject> rssDataObjects) {
		aQuery = new AQuery(this);
		runOnUiThread(new Runnable() {

			public void run() {
				if (rssDataObjects != null) {
					final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					tblLayout.setVisibility(View.VISIBLE);
					loadingpage.setVisibility(View.GONE);
					
					doAddNews(inflater, rssDataObjects, 0);
				}
			}
		});
	}

	OnClickListener onClickListener = new OnClickListener() {

		public void onClick(View v) {
			if (v != null && v.getTag() != null) {
				Logger.logMe("Open link " + v.getTag());
			}

		}
	};

	long requestId = -1;

	void initNewsData() {
		RssRequester rssRequester = new RssRequester(newId, new RequestListener() {

			public void onRequestFailed(String errorMsg, Request request) {
				// TODO Auto-generated method stub

			}

			public void onRequestComplete(Request request) {
				if (request instanceof RssRequester) {
					Logger.logMe(">>>> RssRequester");
					RssRequester r = (RssRequester) request;
					
						initRssData(r.getArrayList());
					
				}
			}
		});
		requestId = AppSettingParams.nextRequestId();
		rssRequester.setRequestId(requestId);
		APIRequestWorker.getInstance().addAPIRequest(rssRequester);
	}

}
